% In this practical we will work with a single subject's EEG dat and perform an ERF
% analysis in sensor space. 
%

%%%%%%%%%%%%%%%%%%
%% SETUP THE MATLAB PATHS
% make sure that fieldtrip and spm are not in your matlab path


clear all; close all;


global OSLDIR;

%tilde='/home/mwoolrich/Desktop';
%tilde='/Users/dantemant/Documents/meeg';
%osldir=[tilde '/osl1.2.beta.14.7dante'];

tilde='/Users/dantemant/Documents/meeg/';
osldir=[tilde '/osl1.2.beta.16dante'];    

addpath(osldir);
osl_startup(osldir);

%%%%%%%%%%%%%%%%%%
%% INITIALISE GLOBAL SETTINGS FOR THIS ANALYSIS

%testdir=[tilde '/meeg_data/meeg_signals/case_1084/111202'];
testdir=['/Users/dantemant/Documents/meeg/meeg_data/meeg_signals/case_1084/111202'];

workingdir=[testdir '/preprocessing']; % this is the directory the SPM files will be stored in

cmd = ['mkdir ' workingdir]; unix(cmd); % make dir to put the results in

clear spm_files_continuous spm_files_epoched;

% set up a list of SPM MEEG object file names (we only have one here)
spm_files_continuous{1}=[workingdir '/CSMCAdfMspm8_eeg1.mat'];
spm_files_epoched{1}=[workingdir '/eCSMCAdfMspm8_eeg1.mat'];

% defining experimental conditions and contrasts to be calculated

% Xsummary is a parsimonious description of the design matrix.
% It contains values Xsummary{reg,cond}, where reg is a regressor no. and cond
% is a condition no. This will be used (by expanding the conditions over
% trials) to croat_settingse the (num_regressors x num_trials) design matrix:

conditions=[{'0'},{'1'},{'2'},{'3'},{'4'},{'5'}, ...
    {'6'},{'7'},{'8'},{'9'},{'10'},{'11'},{'12'}, ...
    {'13'},{'14'},{'15'},...
    {'20'},{'21'},{'22'},{'23'},{'24'},{'25'}, ...
    {'26'},{'27'},{'28'},{'29'},{'30'},{'31'},{'32'}, ...
    {'33'},{'34'},{'35'},...
    {'40'},{'41'},{'42'},{'43'},{'44'},{'45'}, ...
    {'46'},{'47'},{'48'},{'49'},{'50'},{'51'},{'52'}, ...
    {'53'},{'54'},{'55'}];


% conditions=[{0},{1},{2},{3},{4},{5}, ...
%     {6},{7},{8},{9},{10},{11},{12}, ...
%     {13},{14},{15},...
%     {20},{21},{22},{23},{24},{25}, ...
%     {26},{27},{28},{29},{30},{31},{32}, ...
%     {33},{34},{35},...
%     {40},{41},{42},{43},{44},{45}, ...
%     {46},{47},{48},{49},{50},{51},{52}, ...
%     {53},{54},{55}];



Xsummary={};
cond_list=[9 25 41 9-8 25-8 41-8];
for i=1:length(cond_list),
    Xsummary{i}=zeros(size(conditions,2),1);
    Xsummary{i}(cond_list(i))=1;
end

contrast={};
if(0)

    for c=1:16 %
        contrast{c}=zeros(size(conditions,2),1);
        contrast{c}([1+(c-1) 17+(c-1) 33+(c-1)])=1;
    end
    contrast{17}=ones(size(conditions,2),1); % all stim
    contrast{18}=zeros(size(conditions,2),1);
    contrast{18}([9 25 41])=1; % all targets
    contrast{19}=zeros(size(conditions,2),1);
    contrast{19}([9-8 25-8 41-8])=1; % away non-targets
    contrast{20}=zeros(size(conditions,2),1);
    contrast{20}([1:16])=1; % all jitter 1
    contrast{21}=zeros(size(conditions,2),1);
    contrast{21}([17:32])=1;% all jitter 2
    contrast{22}=zeros(size(conditions,2),1);
    contrast{22}([33:48])=1;% all jitter 3
else
    contrast{1}=zeros(length(cond_list),1);
    contrast{1}([1 2 3])=1; % all targets
    contrast{2}=zeros(length(cond_list),1);
    contrast{2}([4 5 6])=1; % away non-targets

end;



%%%%%%%%%%%%%%%%%%%
%% SETUP SENSOR SPACE AND FIRST LEVEL GLM USING OAT

oat=[];
%oat.source_recon.D_continuous=spm_files_continuous;
oat.source_recon.conditions=conditions;
oat.source_recon.D_epoched=spm_files_epoched; % this is passed in so that the bad trials and bad channels can be read out
oat.source_recon.freq_range=[3 35]; % frequency range in Hz
oat.source_recon.time_range=[-0.1 0.5];
oat.source_recon.modalities={'EEG'};
oat.source_recon.method='none';

oat.first_level.design_matrix_summary=Xsummary;

% contrasts to be calculated:
oat.first_level.contrast={};
for i=1:length(contrast)
    oat.first_level.contrast{i}=contrast{i};
    oat.first_level.contrast_name{i}=['C' num2str(i)];
end

oat.first_level.cope_type='acope';

oat = osl_check_oat(oat);

%% LOOK AT OAT SETTINGS
% As well as using the settings we specified in the previous cell, calling osl_check_oat  has filled in a bunch of other settings as well.
oat
oat.source_recon
oat.first_level

%% RUN OAT
oat.to_do=[1 1 0 0];
oat = osl_run_oat(oat);

%% View the GLM design matrix 
% (NOTE that column 1 is motorbikes, columns 2-4 are faces)

% load first-level GLM result
stats1=osl_load_oat_results(oat,oat.first_level.results_fnames{1});

figure;imagesc(stats1.x);title('GLM design matrix');xlabel('regressor no.');ylabel('trial no.');

%% visualise using Fieldtrip
% note that this produces an interactive figure, with which you can:
% - draw around a set of sensors
% - click in the drawn box to produce a plot of the time series
% - on the time series plot you can draw a time window
% - and click in the window to create a topoplot averaged over that time
% window (which is itself interactive....!)

S2=[];
S2.oat=oat;
S2.stats_fname=oat.first_level.results_fnames{1};
S2.modality='EEG';
S2.first_level_contrast=[1 2];

% calculate t-stat using contrast of absolute value of parameter estimates
osl_stats_multiplotER(S2);

%%
% try rerunning the "visualise using Fieldtrip" bit using other
% first_level_contrast's

%%
% now have a go a changing the settings above and re-running with
% oat.first_level.cope_type='cope'
% rather than:
% oat.first_level.cope_type='acope';


